// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
import { ipcRenderer, contextBridge, webUtils } from 'electron'
import { CreateChatProps, OnUpdatedCallback, AppConfig, ProviderName, TestConnectResult, } from './types'

// 实现 interface.d.ts (类型声明) IPC 通信。通过 contextBridge 仅暴露必要的 API，避免直接暴露 ipcRenderer，并转发 IPC 通信到主进程
contextBridge.exposeInMainWorld('electronAPI', {
  startChat: (data: CreateChatProps) => ipcRenderer.send('start-chat', data),
  onUpdateMessage: (callback: OnUpdatedCallback) => ipcRenderer.on('update-message', (_event, value) => callback(value)),
  showContextMenu: (id: number) => ipcRenderer.send('show-context-menu', id),
  onDeleteConversation: (callback: (id: number) => void) => ipcRenderer.on('delete-conversation', (_event, id) => callback(id)),
  getFilePath: (file: File) => webUtils.getPathForFile(file),
  copyImageToUserDir: (sourcePath: string) => ipcRenderer.invoke('copy-image-to-user-dir', sourcePath),
  getConfig: () => ipcRenderer.invoke('get-config'),
  updateConfig: (config: Partial<AppConfig>) => ipcRenderer.invoke('update-config', config),
  onMenuNewConversation: (callback: () => void) => ipcRenderer.on('menu-new-conversation', () => callback()),
  onMenuOpenSettings: (callback: () => void) => ipcRenderer.on('menu-open-settings', () => callback()),
  testProviderConnect: (providerName: ProviderName): Promise<TestConnectResult> => ipcRenderer.invoke('test-provider-connect', providerName),
  onMenuOpenWebsite: (callback: (site: string) => void) => ipcRenderer.on('menu-open-website', (_event, site) => callback(site)),
})

// IPC进程间通信模式：

// 单向通知：ipcRenderer.send / ipcMain.on
// 事件监听: ipcRenderer.on
// 双向调用：ipcRenderer.invoke / ipcMain.handle
// 事件推送（主进程->渲染进程）：webContents.send + ipcRenderer.on